package com.youmu.sichuansfc.controller;

import com.youmu.sichuansfc.common.CommonRes;
import com.youmu.sichuansfc.domain.YoumuUser;
import com.youmu.sichuansfc.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    private Logger logger = Logger.getLogger(LoginController.class);
    @Autowired
    UserService userService;

    /**
     *  登录
     * @param name
     * @param password
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody
    CommonRes login(@RequestParam(name = "name") String name,
                    @RequestParam(name = "password") String password) throws Exception {

        logger.info("name = "+ name + " password = "+ password);
        System.out.println("name = "+ name + " password = "+ password);
        YoumuUser youmuUser = userService.login(name, password);
        if(youmuUser != null) {
            return CommonRes.success(youmuUser);
        }else {
            return  CommonRes.checkFail("账号或密码错误");
        }
    }

    @RequestMapping(value = "/loginTest", method = RequestMethod.POST)
    public @ResponseBody
    CommonRes loginTest() throws Exception {

     return CommonRes.success();

    }
}
